//
//  firsPageViewController.m
//  A段项目音乐播放
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//
#import "firsPageViewController.h"
#import "firstTableViewCell.h"
#import <AFNetworking/AFNetworking.h>
#import "firstModel.h"
#import "MyScrollView.h"
#import "firstTableViewCell.h"
#import "SecondTableViewCell.h"
#import "ThreeTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "hotSongViewController.h"
#import "MyImageView.h"
#import "NewSongViewController.h"
#import "NewMelodyViewController.h"
#import "headPictureViewController.h"
#import "TOHotViewController.h"
#import "SingerModel.h"
#import "PendulumView.h"
#import "MainViewController.h"
#import "NewSongViewController.h"
@interface firsPageViewController ()<UITableViewDataSource,UITableViewDelegate,SecondTableViewCellDelegate,MyScrollViewDelegate>
@property(nonatomic ,retain)UITableView *tableView;
@property (nonatomic ,retain)NSMutableArray *array;
@property (nonatomic , retain)NSMutableArray *scrollArray;
@property (nonatomic ,retain)NSMutableArray *titleArray;
@property (nonatomic ,retain)NSMutableArray *songArray;
@property (nonatomic ,retain)NSMutableArray *individuationArray;
@end

@implementation firsPageViewController
@synthesize leftSwipeGestureRecognizer ,rightSwipeGestureRecognizer;
-(void)dealloc
{
    
    [_array release];
    [_tableView release];
    [_scrollArray release];
    [_songArray release];
    [_individuationArray release];
    [_titleArray release];
    [super dealloc];

}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self)
    {
        self.array =[NSMutableArray array];
        self.scrollArray =[NSMutableArray array];
        self.titleArray =[NSMutableArray array];
        self.songArray =[NSMutableArray array];
        self.individuationArray =[NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor cyanColor];
    [self createRequest];
    UIColor *ballColor = [UIColor colorWithRed:0.47 green:0.60 blue:0.89 alpha:1];
    PendulumView *pendulum = [[PendulumView alloc] initWithFrame:self.view.bounds ballColor:ballColor];
    [self.view addSubview:pendulum];
    
    UIBarButtonItem *button =[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(button:)];
    
    [self.navigationItem setLeftBarButtonItem:button];
    
    self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    
    self.leftSwipeGestureRecognizer.direction =  UISwipeGestureRecognizerDirectionLeft;
    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    
    [self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
    [self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
    
    
}

- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
    /* 设置翻页动画 */
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        NewSongViewController *login =[[NewSongViewController alloc]init];
        CATransition *trans = [CATransition animation];
        [self.navigationController pushViewController:login animated:NO];
        trans.type = @"push";
        trans.duration = 0.8f;
        trans.subtype = @"fromLeft";
        trans.type=@"rippleEffect";
        [self.navigationController.view.layer addAnimation:trans forKey:nil];
        [login release];
     
    }
    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        /* 设置翻页动画 */
        
        MainViewController *firs =[[MainViewController alloc]init];
        [self.navigationController pushViewController:firs animated:YES];
        CATransition *tansition =[CATransition animation];
        tansition.type =@"push";
        tansition.duration=0.8f;
        tansition.type=@"rippleEffect";
        [self.navigationController.view.layer addAnimation:tansition forKey:nil];
        [firs release ];
    }
    
}
-(void)button:(id)but
{
    MainViewController * one = [[MainViewController alloc]init];
    [self.navigationController popViewControllerAnimated:YES];
    [one release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

-(void)createRequest
{
    AFHTTPRequestOperationManager *manger =[AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer =[AFCompoundResponseSerializer serializer];
    
    __block firsPageViewController *firspage =self;
    
    [manger GET:@"http://online.dongting.com/recomm/recomm_modules?app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=8.3&active=0&net=2&openudid=826886cd55c251bacd39b2d6523c12b126f3c590&idfa=27D84D88-B1C6-4173-B7C5-EE78CD26E7FB&utdid=VVrl99FV5JUDAA1STsAtAkrx&alf=201200&bundle_id=com.ttpod.music&version=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *firstDic =[dic objectForKey:@"data"];
        NSArray *arr =[firstDic objectForKey:@"songlists"];
        for (NSDictionary *titledic  in arr)
        {
            [self.titleArray addObject:titledic];
        }
        
        /* scroll数据解析 */
        
        NSDictionary *seconddic =[ arr objectAtIndex:0];
        NSArray *arra =[seconddic objectForKey:@"data"];

        for ( NSDictionary *therr in arra)
        {
                firstModel *model =[[firstModel alloc]init];
//             [model setValue:[dic2 objectForKey:@"singer_name"] forKey:@"singer_name"];
            NSDictionary *dic2 =[therr objectForKey:@"action"];
            
            [model setValue:[dic2 objectForKey:@"value"] forKey:@"value"];
            
            [model setValuesForKeysWithDictionary:therr];
            
                [self.scrollArray addObject:model];
        }
        
        /* indexpath.row==0 indexpath.row==1 数据*/
        
        NSDictionary *secondDic =[ arr objectAtIndex:1];
        NSArray *arra1 =[secondDic objectForKey:@"data"];
        
        for ( NSDictionary *therr1 in arra1) {
            firstModel *model1 =[[firstModel alloc]init];
            [model1 setValuesForKeysWithDictionary:therr1];
            [self.array addObject:model1];
        }
        
        /* 热门歌单数据 */
        
        NSDictionary *threeDic =[ arr objectAtIndex:2];
        NSArray *arra2 =[threeDic objectForKey:@"data"];
        
        for ( NSDictionary *therr2 in arra2) {
            firstModel *model =[[firstModel alloc]init];
            [model setValuesForKeysWithDictionary:therr2];
             [self.songArray addObject:model];
       
        }
        
        if ( [arr objectAtIndex:5]>[arr objectAtIndex:4] ) {
            
            NSDictionary *fourDic =[ arr objectAtIndex:5];
            NSArray *arra3 =[fourDic objectForKey:@"data"];
            for ( NSDictionary *therr3 in arra3)
            {
                firstModel *model =[[firstModel alloc]init];
                [model setValuesForKeysWithDictionary:therr3];
                NSDictionary *dic2 =[therr3 objectForKey:@"action"];
                [model setValue:[dic2 objectForKey:@"value"] forKey:@"value"];
                [self.individuationArray addObject:model];
                
            }
            [firspage.tableView  reloadData];
            
            [firspage createTableView];
        }
        
        else
        
        {
        
            NSDictionary *fourDic =[ arr objectAtIndex:5];
            NSArray *arra3 =[fourDic objectForKey:@"data"];
            for ( NSDictionary *therr3 in arra3)
            {
                firstModel *model =[[firstModel alloc]init];
                [model setValuesForKeysWithDictionary:therr3];
                NSDictionary *dic2 =[therr3 objectForKey:@"action"];
                [model setValue:[dic2 objectForKey:@"value"] forKey:@"value"];
                [self.individuationArray addObject:model];
            }
            [firspage.tableView  reloadData];
            [firspage createTableView];
        }
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        NSLog(@"error=%@",error);
    }];
}

-(void)createTableView
{
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate =self;
    self.tableView.dataSource=self;
    MyScrollView *my =[[MyScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    
    my.delegate =self;
    [my setImages:self.scrollArray firstModel:nil];
    [self.tableView setTableHeaderView:my];
    [self.tableView registerClass:[SecondTableViewCell class]forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    [self.tableView setSeparatorColor:[UIColor whiteColor]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count]+self.titleArray.count-2;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.row ==0) {
        static NSString * str =@"dsa";
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        NSDictionary *dic =[self.titleArray objectAtIndex:1];
        cell.textLabel.text=[dic objectForKey:@"desc"];
        return cell;
    }
    if (indexPath.row==1)
    {
        static NSString * str =@"dsa";
        firstTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            
            cell =[[firstTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
        }
          cell.selectionStyle=UITableViewCellSelectionStyleNone;
        firstModel *model =[self.array objectAtIndex:0];
        [cell sendModel:model];
        return cell;
    }
    
    if (indexPath.row ==2)
    {
        static NSString * str =@"dsa";
        firstTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
        
        if (!cell) {
            cell =[[firstTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        firstModel *model =[self.array objectAtIndex:1];
        [cell sendModel:model];
        return cell;
    }
    if (indexPath.row ==3) {
        static NSString * str =@"dsa";
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        NSDictionary *dic =[self.titleArray objectAtIndex:2];
        cell.textLabel.text=[dic objectForKey:@"desc"];
        return cell;
    }
    if (indexPath.row ==4)
    {
        SecondTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        firstModel *rightModel =[self.songArray objectAtIndex:0];
        firstModel *midModel =[self.songArray objectAtIndex:1];
        firstModel *leftModel =[self.songArray objectAtIndex:2];
        [cell setrightModel:rightModel midModel:midModel leftModel:leftModel];
        cell.delegate =self;
        return cell;
    }
    
    if (indexPath.row ==5) {
        static NSString * str =@"dsadabvb";
        
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
                if (!cell) {
            
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        NSDictionary *dic =[self.titleArray objectAtIndex:4];
        
        cell.textLabel.text=[dic objectForKey:@"name"];
        
        return cell;
    }
    
    static NSString * str =@"dsadas";
    
    ThreeTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
    
    if (!cell) {
        
        cell =[[ThreeTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    SingerModel *model =[self.individuationArray objectAtIndex:indexPath.row-6];
    
    [cell sendModel:model];
   
    return cell;
 
}

-(void)imageAction:(MyImageView *)sender

{
    hotSongViewController *hotsong =[[hotSongViewController alloc]init];
    hotsong.model = sender.model;
    [self.navigationController pushViewController:hotsong animated:YES];
    CATransition *trans = [CATransition animation];
    trans.type = @"push";
    trans.duration = 0.8f;
    trans.type=@"rippleEffect";
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
}
-(void)setimageView:(MyImageView *)touch

{
    NSString *str =touch.model.value;
    
    if ([str hasPrefix:@"http"])
    {
    
    headPictureViewController *head =[[headPictureViewController alloc]init];
    head.MODEL = touch.model;
    [self.navigationController pushViewController:head animated:YES];
    CATransition *trans = [CATransition animation];
    trans.type = @"push";
    trans.duration = 0.8f;
    trans.type=@"rippleEffect";
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
  
    }
    else
    {
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"提  示" message:@"暂无更新" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        
        [aler show];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.row==0) {
        
        return 30;
        
    }
    
    if (indexPath.row==1) {
        
        return 140;
    }

    if (indexPath.row==2) {
        
        return 140;
        
    }
    if (indexPath.row==3) {
        
        return 30;
        
    }

    if (indexPath.row==4) {
        
        return 160;
        
    }
    
    if (indexPath.row==5) {
        return 30;
    }
    
    return 140;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.row==1)
    {
       NewMelodyViewController *melody =[[[NewMelodyViewController alloc]init] autorelease];
        [self.navigationController pushViewController:melody animated:YES];
        CATransition *trans = [CATransition animation];
        SingerModel *model =[self.array objectAtIndex:0];
        melody.model =model;
        trans.type = @"push";
        trans.duration = 0.8f;
        trans.subtype = @"fromLeft";
        trans.type=@"rippleEffect";
        [self.navigationController.view.layer addAnimation:trans forKey:nil];
    }
    if (indexPath.row ==2)
    {
        NewSongViewController *newSong =[[[NewSongViewController alloc]init] autorelease];
        SingerModel *model =[self.array objectAtIndex:1];
        newSong.model = model;
        [self.navigationController pushViewController:newSong animated:YES];
        CATransition *trans = [CATransition animation];
        trans.type = @"push";
        trans.duration = 0.8f;
        trans.subtype = @"fromLeft";
        trans.type=@"rippleEffect";
        [self.navigationController.view.layer addAnimation:trans forKey:nil];
   
    }

    if (indexPath.row>6)
    {
        TOHotViewController *newSong =[[[TOHotViewController alloc]init] autorelease];
        SingerModel *model =[self.individuationArray objectAtIndex:indexPath.row-6];
        newSong.model =model;
        [self.navigationController pushViewController:newSong animated:YES];
        CATransition *trans = [CATransition animation];
        trans.type = @"push";
        trans.duration = 0.8f;
        trans.subtype = @"fromLeft";
        trans.type=@"rippleEffect";
        [self.navigationController.view.layer addAnimation:trans forKey:nil];
 
    }
    
}
@end
